package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.saas.domain.dao.CustomerStatementStatisticsDao;
import com.xbongbong.saas.domain.entity.CustomerStatementStatisticsEntity;
import com.xbongbong.saas.model.CustomerStatementStatisticsModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description
 * @author: shian
 * @date : 2020/12/30
 */
@Service("customerStatementStatisticsModel")
public class CustomerStatementStatisticsModelImpl implements CustomerStatementStatisticsModel {
    @Resource
    private CustomerStatementStatisticsDao customerStatementStatisticsDao;
    @Resource
    private PaasEsModel paasEsModel;

    @Override
    public Integer insert(CustomerStatementStatisticsEntity entity) throws XbbException {
        long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        entity.setDel(DelEnum.NORMAL.getDel());

        Integer success = customerStatementStatisticsDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_STATEMENT);
        paasEsModel.insert(insertDTO);
        return success;
    }

    @Override
    public void insertBatch(List<CustomerStatementStatisticsEntity> list) throws XbbException{
        customerStatementStatisticsDao.insertBatch(list);
        //ES数据插入
        List<InsertDTO> insertList = new ArrayList<>();
        for (PaasFormDataEntity item : list) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(item.getCorpid() + "_" + item.getId());
            item.setDataId(item.getId());
            insertDTO.setSource(BeanUtil.convertBean2Map(item, true, true));
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_STATEMENT);
            insertList.add(insertDTO);
        }
        paasEsModel.insertBatch(insertList);
    }

    @Override
    public List<CustomerStatementStatisticsEntity> findEntitys(Map<String, Object> param) {
        return customerStatementStatisticsDao.findEntities(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return customerStatementStatisticsDao.getEntitiesCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer update(CustomerStatementStatisticsEntity entity) throws XbbException{
        entity.setUpdateTime(DateTimeUtil.getInt());
        Integer success = customerStatementStatisticsDao.update(entity);
        if (Objects.equals(1, success)) {
            // 更新es
            PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
            BeanUtil.copyProperties(entity, paasFormDataEntity);
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_STATEMENT);
            paasEsModel.update(insertDTO);
        }
        return success;
    }

    @Override
    public CustomerStatementStatisticsEntity getByKey(Long key, String corpid) {
        return customerStatementStatisticsDao.getByKey(key, corpid);
    }

    @Override
    public List<CustomerStatementStatisticsEntity> getbyIds(List<Long> dataIdList, String corpid) {
        return customerStatementStatisticsDao.getbyIds(dataIdList, corpid);
    }

    @Override
    public void updateBatch(List<UpdateDataEntity> updateDataList, String corpid) throws XbbException {
        customerStatementStatisticsDao.updateBatch(updateDataList, corpid);
        paasEsModel.updateBatchMuchField(updateDataList, IndexTypeEnum.IDX_SAAS_STATEMENT);
    }

    @Override
    public Integer deleteBatch(List<Long> dataIdList, String corpid) throws XbbException {
        Integer integer = customerStatementStatisticsDao.deleteBatch(dataIdList, corpid);
        paasEsModel.deleteBatch(dataIdList, corpid, DelEnum.DELETE.getDel(), IndexTypeEnum.IDX_SAAS_STATEMENT);
        return integer;
    }

    @Override
    public Long save(CustomerStatementStatisticsEntity statisticsEntity) throws XbbException {
        if (statisticsEntity.getId() == null || statisticsEntity.getId() == 0) {
            insert(statisticsEntity);
        } else {
            update(statisticsEntity);
        }
        return statisticsEntity.getId();
    }
}
